package com.my.mytest.util;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * Created by Android Studio.
 * User: mwb
 * Date: 2020/12/8 0008
 * Time: 下午 1:52
 * Describe:
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int space;
    private int columnCount;

    public SpaceItemDecoration(Context context, int space, int columnCount) {
        this.space = px2dip(context, space);
//        this.space = space;
        this.columnCount = columnCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
//        if (parent.getChildViewHolder(view) instanceof HomeAdapter.FormalThreeVH) {}
        outRect.top = space;
        //瀑布流专属分割线
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        /**
         * 根据params.getSpanIndex()来判断左右边确定分割线
         * 第一列设置左边距为space，右边距为space/2  （第二列反之）
         */
        if (params.getSpanIndex() % 2 == 0) {
            outRect.left = space;
            outRect.right = space / 2;
        } else {
            outRect.left = space / 2;
            outRect.right = space;
        }

//        outRect.left = space;
//        outRect.right = space;
//        if (columnCount != 1) {
//            StaggeredGridLayoutManager.LayoutParams params =
//                    (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
//            // 当前位置
//            int position = params.getSpanIndex();
//            int X = columnCount - position;
//            if (X == columnCount) {
//                // 最左边
//                outRect.right = space;
//            } else if (X == 1) {
//                // 最右边
//                outRect.left = space;
//            } else {
//                outRect.left = space;
//                outRect.right = space;
//            }
//        }
//        outRect.bottom = space;


//        StaggeredGridLayoutManager.LayoutParams params =
//                    (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
//        // 当前位置
//        int position = params.getSpanIndex();
//        int X = columnCount - position;
//        if(X == columnCount){
//            // 最左边
//            outRect.left = space;
//            outRect.right = space;
//        }else if(X == 1){
//            // 最右边
//            outRect.left = space;
//            outRect.right = space;
//        }else{
//            outRect.left = space;
//            outRect.right = space;
//        }
//        outRect.bottom = space;


    }

    // 网格布局使用
//    @Override
//    public void getItemOffsets(Rect outRect, View view,
//                               RecyclerView parent, RecyclerView.State state) {
//        outRect.left = space;
//        outRect.right = space;
//        if (columnCount != 1) {
//            int position;
//            if(parent.getLayoutManager() instanceof StaggeredGridLayoutManager){
//                StaggeredGridLayoutManager.LayoutParams params =
//                        (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
//                // 当前位置
//                position = params.getSpanIndex();
//
//            }else{
//                GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
//                position = params.getSpanIndex();
//            }
//            int X = columnCount - position;
//            if (X == columnCount) {
//                // 最左边
//                outRect.right = space;
//            } else if (X == 1) {
//                // 最右边
//                outRect.left = space;
//            } else {
//                outRect.left = space;
//                outRect.right = space;
//            }
//        }
//        outRect.bottom = space;
//    }


    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
